// pages/my/my.js
import Toast from '../../dist/toast/toast';
var hlshost = "https://kaoshi.diuber.cn/PHPmvc/index.php?";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isLogin: false,
        canIUseGetUserProfile: false,
        show: false,
        questionnum: 0,
        avgscore: 0,
        actions: [{
                name: '复制开发者微信号',
            },
            {
                name: '复制开发者QQ号',
            },
        ],
        userInfo: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //this.login();

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    clear() {
        var that = this;
        wx.showModal({
            title: '温馨提示',
            content: '确认清空缓存吗？',
            complete: (res) => {

                if (res.confirm) {
                    //清空类别category_id=2的题库答题记录
                    wx.removeStorageSync("orderind2");
                    wx.removeStorageSync("orderlist2");
                    wx.removeStorageSync("order2");
                    //清空类别category_id=5的题库答题记录
                    wx.removeStorageSync("orderind5");
                    wx.removeStorageSync("orderlist5");
                    wx.removeStorageSync("order5");
                    wx.showToast({
                        title: '清除成功',
                    })
                }
            }
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        wx.hideLoading();
        this.setData({
            userInfo: wx.getStorageSync('userInfo'),
        })
        var that = this;
        wx.request({
            url: hlshost + "c=myanswer&m=index",
            method: 'POST',
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
                uid: wx.getStorageSync('userInfo').uid
            },
            success: function (res) {
                //console.log(res.data);
                if (res.data.code == 0) {
                    that.setData({
                        questionnum: res.data.myAnswerNum,
                        avgscore: res.data.avgscore,
                    })
                }
            },
            fail: function (err) {
                wx.showToast({
                    title: '查询答题情况失败，请稍后重试',
                    icon: 'none',
                    duration: 2000
                });
            }
        });

    },

    login() {
        var that = this;
        wx.showLoading({
            title: '登陆中',
        })
        var uid = wx.getStorageSync('userInfo').uid;
        var phone = wx.getStorageSync('userInfo').phone;
        wx.login({
            success(res) {
                if (res.code) {
                    console.log(res.code)
                    wx.Apis.login.login(res.code, (code, data) => {
                        console.log(data);
                        console.log(data.openid);
                        console.log(data.nickName);
                        wx.Apis.setUid(data.openid); //openid
                        wx.Apis.set('openid', data.openid);
                        wx.setStorageSync('userInfo', data);
                        that.setData({
                            userInfo: data
                        });
                        wx.hideLoading()
                        if (data.nickName === null) {
                            wx.navigateTo({
                                url: '/pages/login/login',
                            })
                        }
                    });
                }
            }
        });

    },

    goRecord() {
        if (this.data.userInfo.nickName == undefined || this.data.userInfo.nickName == '') {
            this.login()
            return false;
        }
        wx.navigateTo({
            url: '/pages/record/record',
        })
    },

    goPoint() {
        if (this.data.userInfo.nickName == undefined || this.data.userInfo.nickName == '') {
            this.login()
            return false;
        }
        wx.navigateTo({
            url: '/pages/point/point',
        })
    },

    goCode() {
        if (this.data.userInfo.nickName == undefined || this.data.userInfo.nickName == '') {
            this.login()
            return false;
        }
        wx.navigateTo({
            url: '/pages/code/code',
        })
    },

    goSetting() {
        if (this.data.userInfo.nickName == undefined || this.data.userInfo.nickName == '') {
            this.login()
            return false;
        }
        wx.navigateTo({
            url: '/pages/setting/setting',
        })
    },

    goSign() {
        if (this.data.userInfo.nickName == undefined || this.data.userInfo.nickName == '') {
            this.login()
            return false;
        }
        wx.navigateTo({
            url: '/pages/sign/sign',
        })
    },

    goUserXy() {
        wx.navigateTo({
            url: '/pages/userXy/userXy',
        })
    },

    goYinsiXy() {
        wx.navigateTo({
            url: '/pages/yinsiXy/yinsiXy',
        })
    },

    goFeedback() {
        if (this.data.userInfo.nickName == undefined || this.data.userInfo.nickName == '') {
            this.login()
            return false;
        }
        wx.navigateTo({
            url: '/pages/feedback/feedback',
        })
    },
    goAbout() {
        this.setData({
            show: true
        });
    },
    clearStorage() {
        let that = this
        wx.showModal({
            content: '退出登录会清空答题记录缓存，确定要清空吗？',
            success: function (a) {
                if (a.confirm) {
                    // 清空本地储存的 openid
                    wx.removeStorageSync('openid');
                    wx.removeStorageSync('userInfo');
                    that.setData({
                        userInfo: ''
                    })
                }
            }
        })
    },
    onClose() {
        this.setData({
            show: false
        });
    },
    onSelect(event) {
        console.log(event.detail)
        var that = this
        if (event.detail.name === "复制开发者QQ号") {
            wx.setClipboardData({
                data: '903363777',
                success: function (res) {

                }
            })
        } else {
            wx.setClipboardData({
                data: 'kossfirst',
                success: function (res) {

                }
            })
        }
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            title: "新⽼司机宝典，⽹约⻋好帮⼿！",
            path: "pages/index/index",
            imageUrl: "/images/share.png"
        };
    },
})